package com.michael.demos.springboot.valid.controller;

import com.michael.demos.springboot.common.result.JsonResult;
import com.michael.demos.springboot.common.result.R;
import com.michael.demos.springboot.valid.groups.F1;
import com.michael.demos.springboot.valid.groups.F12;
import com.michael.demos.springboot.valid.groups.F2;
import com.michael.demos.springboot.valid.model.FruitBasket;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 类功能描述:
 * <pre>
 *   Demo
 *
 *   PS: @Validated 加到类上， GET参数才会启用校验
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/7/29 10:24
 */
@Validated
@RestController
public class DemoController {

    @GetMapping("/test")
    public JsonResult testGetParams(@NotBlank(message = "需要参数 a") @RequestParam String a, @RequestParam String b) {
        return R.succeed();
    }

    @GetMapping("/test1")
    public JsonResult testGetParams1(@NotBlank(message = "需要参数 a") String a, @NotNull(message = "需要参数 b") Integer b) {
        return R.succeed();
    }


    @PostMapping("/test")
    public JsonResult testPostParams(@Validated @RequestBody FruitBasket fruitBasket) {
        return R.succeed();
    }

    @PostMapping("/test1")
    public JsonResult testPostParams1(@Validated({F1.class}) @RequestBody FruitBasket fruitBasket) {
        return R.succeed();
    }

    @PostMapping("/test2")
    public JsonResult testPostParams2(@Validated({F2.class}) @RequestBody FruitBasket fruitBasket) {
        return R.succeed();
    }

    @PostMapping("/test3")
    public JsonResult testPostParams3(@Validated({F12.class}) @RequestBody FruitBasket fruitBasket) {
        return R.succeed();
    }

}
